package org.abettor.octopus.ui;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

/**
 * “关于”对话框类
 * @author abettor
 *
 */
public class AboutDialog extends Dialog
{
	private final static String TITLE = "About Octopus Universal Database Cracker ...";
	private final static String TEXT = "Octopus Universal Database Cracker is a multi-threads password cracker for any type of database you want .";
	private final static String ICON = "./img/logo.png";
	private final static int WIDTH = 430;
	private final static int HEIGHT = 170;

	/**
	 * @see org.eclipse.swt.widgets.Dialog#Dialog(Shell)
	 */
	public AboutDialog(Shell parent)
	{
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		setText(TITLE);
	}

	/**
	 * 显示对话框界面
	 *
	 */
	public void show()
	{
		Shell parent = getParent();
		Display display = parent.getDisplay();
		Image img = new Image(display, ICON);
		Font fontLabel = new Font(display, "Sans", 10, SWT.NORMAL);
		Font fontButton = new Font(display, "Sans", 10, SWT.NORMAL);
		Monitor monitor = display.getPrimaryMonitor();
		Rectangle rect = monitor.getBounds();

		//	创建对话框窗口
		final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setText(getText());
		shell.setSize(WIDTH, HEIGHT);
		shell.setLocation((rect.width - WIDTH) / 2, (rect.height - HEIGHT) / 2);

		//	显示图标
		Label imgLabel = new Label(shell, SWT.SHADOW_NONE);
		Rectangle imgRect = img.getBounds();
		imgLabel.setSize(imgRect.width, imgRect.height);
		imgLabel.setImage(img);
		imgLabel.setLocation(20, 30);

		//	显示文本信息
		Label label = new Label(shell, SWT.SHADOW_NONE | SWT.WRAP);
		label.setText(TEXT);
		label.setLocation(70, 20);
		label.setSize(350, 60);
		label.setFont(fontLabel);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String year = sdf.format(new Date());

		//	显示版权信息
		label = new Label(shell, SWT.SHADOW_NONE | SWT.WRAP);
		label.setText("Copyright (C) " + year);
		label.setLocation(120, 83);
		label.setSize(130, 20);
		label.setFont(fontLabel);

		Link link = new Link(shell, SWT.NONE);
		link.setText("<A>abettor</A>");
		link.setToolTipText("Open abettor's home page");
		link.setLocation(255, 81);
		link.setSize(60, 20);
		link.setFont(fontLabel);
		link.addMouseListener(new MouseAdapter()
		{
			public void mouseDown(MouseEvent e)
			{
				Desktop desktop = Desktop.getDesktop();
				try
				{
					desktop.browse(new URI("http://www.abettor.org/"));
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
				}
				catch (URISyntaxException e1)
				{
					e1.printStackTrace();
				}
			}
		}
		);

		//	显示关闭按钮
		Button button = new Button(shell, SWT.CENTER);
		button.setText("Close");
		button.setLocation(320, 110);
		button.setSize(80, 25);
		button.setFocus();
		label.setFont(fontButton);
		button.addMouseListener(new MouseAdapter()
		{
			public void mouseDown(MouseEvent e)
			{
				shell.close();
			}
		}
		);

		//	开始窗口消息循环
		shell.open();
		while (! shell.isDisposed())
		{
			if (! display.readAndDispatch())
			{
				display.sleep();
			}
		}

		//	释放资源
		img.dispose();
		fontButton.dispose();
		fontLabel.dispose();
		shell.dispose();
	}
}
